<?
//Kernel 0.3.8
class kernel{
    public $site;          //Site data
    public $page;          //Page data
    public $link;          //Link data
    public $lang;          //Language data
    public $vars;          //Vars that will be used in temlate
    public $libraries;     //Necessary libraries
    public $symbionts;     //Necessary symbionts
    public $processes;     //Necessary processes
    public function __construct(){
        require_once('libraries/set.lib.php');
        require_once('libraries/dictionary.lib.php');
        require_once('libraries/processes.lib.php');
        $this->site=new dictionary();
        $this->page=new dictionary();
        $this->link=new dictionary();
        $this->lang=new dictionary();
        $this->vars=new dictionary();
        $this->libraries=new set();
        $this->symbionts=new set();
        $this->processes=new processes();
        //Include libraries
        $this->libraries->add(array('kernel', 'set', 'dictionary', 'processes'));
        $this->libraries->pointer_set(2);
        $this->libraries->add(array('user', 'mysql', 'design', 'label', 'message', 'data', 'position', 'symbiont', 'symbionts'));
        $this->inc('l');
        //Connect to db
        global $sql;
        ob_start();
        require_once('config.php');
        ob_clean();
        $sql=new mysql($host, $user, $pass, $db);
        //Create symbionts
        global $sym;
        $sym=new symbionts();
        //Processes
        $procs=$sql->select_each("processes", array('process', 'privilege'), "", "", "", "");
        if(isset($procs[0])&&is_array($procs[0])){
            foreach($procs as $val){
                $this->processes->add($val['process'], $val['privilege']);
            }
        }
        elseif(is_array($procs)){
            $this->processes->add($procs['process'], $procs['privilege']);
        }
        //Collecting site data
        $r=$sql->select_each("config", array("name", "value"));
        $tmp=array();
        foreach($r as $val){
            $v=$val['value'];
            if($v==='true') $v=true;
            else if($v==='false') $v=false;
            $tmp[$val['name']]=$v;
        }
        $this->site->add($tmp);
        $this->site->home=$this->home_page();
        $this->site->files=$this->site->url.'design/'.$this->site->design.'/files/';
        $this->site->js=$this->site->files.'js/';
        $this->site->css=$this->site->files.'css/';
        $this->site->img=$this->site->files.'img/';
        //Processing current url
        $this->url();
        //Collecting page data
        global $user;
        $current_page=$sql->select_each("pages", "*", array('name'=>$this->link->get('page')), "", "", "1");
        if(is_array($current_page)){
            $this->page->add($current_page);
        }
        else{
            $page_404=$sql->select_each("pages", "*", array('is_404'=>1), "", "", "1");
            $this->page->add($page_404);
        }
        //Collecting language data
        $tmp=$sql->select_each("languages", "*", array('abbr'=>$this->link->get('language')), "", "", "1");
        $this->lang->add($tmp);
        $this->lang->set('default', $this->default_language());
        $this->site->base=$this->site->url.($this->site->mlm?$this->lang->abbr.'/':'');
        //Generating vars
        $this->vars->union($this->site, "site_");
        $this->vars->union($this->page, "page_");
        $this->vars->union($this->link, "link_");
        $this->vars->union($this->lang, "lang_");
    }
    //Running kernel
    public function run(){
        global $user, $sql;
        $this->processes->run();
        if($user->access_level<$this->page->access_level||!$this->page->is_active){
            $page_404=$sql->select_each("pages", "*", array('is_404'=>1), "", "", "1");
            $this->page->add($page_404);
        }
        if($this->page->title_lid){
            $l=new label($this->page->title_lid);
            $this->page->title=$l->get();
            $this->vars->page_title=$l->get();
        }
    }
    //Return the home page name
    private function home_page(){
        global $sql;
        return $sql->select_each("pages", "name", array("is_home"=>1), "", "", "1");
    }
    //Return the default language abbreviation
    private function default_language(){
        global $sql;
        return $sql->select_each("languages", "abbr", array("is_default"=>1), "", "", "1");
    }
    //Collect current url
    private function url(){
        global $sql;
        if(isset($_GET['page'])){
            $page=$_GET['page'];
            $suf1 = substr($page, -4);
            $suf2 = substr($page, -1);
            if($suf1!=".html"&&$suf2!="/"&&$suf2!="]"){            //	header("location: ".$_SERVER['REQUEST_URI']."/");            }
            $tok = strtok($page, "/");
            $i=0;
            $j=1;
            $this->link->set('full', $page);
            if($this->site->mlm){
                $pos=strpos($page, "/");
                if($pos){
                    $this->link->without_language=substr($page, $pos+1);
                }
                else{
                    $this->link->without_language='';
                }
            }
            while($tok){
                if($i==0){
                    if($this->site->mlm){
                        if($tok){
                            if($sql->num_rows("languages", array("abbr"=>$tok), 1)){
                                $language=$tok;
                            }
                            else{
                                $language=$this->default_language();
                                $this->link->set('page', $tok);
                            }
                        }
                        else{
                            $language=$this->default_language();
                            $this->link->set('page', $this->home_page());
                        }
                        $this->link->set('language', $language);
                    }
                    else{
                        $this->link->set('language', $this->default_language());
                        $this->link->set('page', $tok!=""?$tok:$this->home_page());
                    }
                }
                elseif($i==1&&($this->site->mlm)&&!$this->link->page){
                    $this->link->set('page', $tok!=""?$tok:$this->home_page());
                }
                else{
                    $this->link->set('param'.$j++, $tok);
                }
                $tok = strtok("/");
                ++$i;
            }
            if(!$this->link->page) $this->link->page=$this->home_page();
        }
        else{
            $this->link->set('page', $this->home_page());
            $this->link->set('language', $this->default_language());
        }
        if(isset($_GET['modifier'])){
            $this->link->set('modifier', $_GET['modifier']);
        }
        else{
            $this->link->set('modifier', "");
        }
    }
    //Include symbionts and|or libraries
    public function inc($what="*"){
        if($what=="*"||$what=="s"||$what=="sybionts"){
            while($s=$this->symbionts->read()){
                $n=explode('-', $s);
                if(count($n)>1){
                    $symbiont=$n[0];
                    $class=$n[1];
                }
                else{
                    $symbiont=$class=$s;
                }
                if(file_exists('symbionts/'.$symbiont.'/'.$class.'.sym.php')){
                    include_once('symbionts/'.$symbiont.'/'.$class.'.sym.php');
                    global $sym;
                    $sc=$symbiont!=$class?$symbiont."_".$class:$symbiont;
                    $sym->add($sc, $symbiont, $class);
                }
                else{
                   $m=new message("Симбионт ".$symbiont." не найден.", "error", true);
                }
            }
        }
        if($what=="*"||$what=="l"||$what=="libraries"){
            while($l=$this->libraries->read()){
                if(file_exists('libraries/'.$l.'.lib.php')){
                    require_once('libraries/'.$l.'.lib.php');
                }
                else{
                    $m=new message("Библиотека ".$l." не найдена.", "error", true);
                }
            }
        }
    }
    public function symbiont_exists($name){
        if(!is_array($name)){
            $s=explode('-', $name);
            if(isset($s[0])&&isset($s[1])){
                $name=$s[0];
                $class=$s[1];
            }
            else{
                $class=$name;
            }
        }
        else{
            if(isset($name[0])&&isset($name[1])){
                $name=$name[0];
                $class=$name[1];
            }
            else{
                $class=$name[0];
            }
        }
        if(file_exists('symbionts/'.$name.'/'.$class.'.sym.php')) return true;
        return false;
    }
    public function libraries_exists($name){
        if(file_exists('libraries/'.$name.'.lib.php')) return true;
        return false;
    }
    public function symbiont_add($name){
        $this->symbionts->push($name);
        $this->inc('s');
    }
    public function library_add($name){
        $this->libraries->push($name);
        $this->inc('l');
    }
};
?>